package ru.mipt.testload.util;

import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.parser.XSOMParser;
import org.jdesktop.swingx.JXTreeTable;
import org.w3c.dom.Document;

import javax.swing.*;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.awt.*;
import java.io.File;
import java.util.Iterator;

/**
 * @author Pavel G. Belevich
 *         Date: 05.06.2010
 *         Time: 22:25:52
 */
public class EditorPanel extends JPanel {

    private JXTreeTable treeTable;

    public EditorPanel() {
        super(new BorderLayout());
        initComponents();
//        configureComponents();
//        DemoUtils.injectResources(this);
//        bind();
    }

    private void initComponents() {
        try {
            XSOMParser parser = new XSOMParser();
            File schemaFile = new File(EditorPanel.class.getClassLoader().getResource("ru/mipt/testload/message.xsd").getFile());
            parser.parse(schemaFile);
            XSSchemaSet xsSchemaSet = parser.getResult();
            Iterator itr = xsSchemaSet.iterateSchema();
            while (itr.hasNext()) {
                XSSchema xsSchema = (XSSchema) itr.next();
                if ("http://mipt.ru/testload".equals(xsSchema.getTargetNamespace())) {

                    SchemaFactory schemaFactory =
                            SchemaFactory.newInstance(
                                    XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    Schema schema = schemaFactory.newSchema(schemaFile);

                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    factory.setSchema(schema);
                    factory.setValidating(true);
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    Document document = builder.newDocument();

                    treeTable = new XsdTreeTable(new XsdTreeTableModel(xsSchema, document));
                    treeTable.setName("componentTreeTable");
                    add(new JScrollPane(treeTable));
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("XML Editor");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new EditorPanel());
                frame.setPreferredSize(new Dimension(800, 600));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
